<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_SALESPAYMNT';
$path_to_root = "..";
include_once($path_to_root . "/includes/ui/allocation_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include("/includes/db/uang_muka_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
require_once($path_to_root .'/../FirePHPCore/fb.php');

$js = "";
if ($use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}
if ($use_date_picker) {
	$js .= get_js_date_picker();
}
add_js_file('payalloc.js');

$isDP  = true;
if (isset($_GET['mode']) || isset($_POST['mode']))
{
	$isDP = $_GET['mode'] == 'dp' || $_POST['mode'] == 'dp';
}
else 
	$isDP = false;
if ($isDP)
{
page(_($help_context = "Pembayaran Uang Muka"), false, false, "", $js);
$_POST['mode'] = 'dp';
}
else
{ 
page(_($help_context = "Pembayaran Pelunasan"), false, false, "", $js);
$_POST['mode'] = 'lunas';
}

if(!isPosibleFinish($isDP))
{
display_error( _("No item for finishing."));
exit;
}

if (!isset($_POST['order_no']) && isset($_POST['AddPaymentItem']))
{
	display_error(_("Invalid order number."));
	unset($_POST['AddPaymentItem']);
}


if (isset($_GET['AddedID'])) {
	$payment_no = $_GET['AddedID'];

	display_notification_centered(_("The customer payment has been successfully entered."));

	submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');

	display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));

//	hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");

	hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
	
	display_footer_exit();
}
elseif (isset($_GET['UpdatedID'])) {
	$payment_no = $_GET['UpdatedID'];

	display_notification_centered(_("The customer payment has been successfully updated."));

	submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');

	display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));

//	hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");

	hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
	
	display_footer_exit();
}

//----------------------------------------------------------------------------------------------

function can_process()
{
	global $Refs;
	global $isDP;
global $arr;
	if (!isset($_POST['order_no']))
	{
		display_error(_("There is no order number."));
		//set_focus('customer_id');
		return false;
	}
	
	if (get_post('amount') <= 0)
	{
		display_error(_("invalid amount."));
		set_focus('amount');
		return false;
	} 
	display_error($isDP);
	fb($arr);
	fb($isDP);
	
	if ($isDP)
	{		
//		display_error(input_num('amount'));
//		exit;
		$syarat = $arr['total'] / 2;
		fb($syarat);
		//display_error($syarat);
		//display_error($arr['total']);
		if($syarat > input_num('amount'))
		{
		display_error(_("Pembayaran minimal 50%"));
		set_focus('amount');
		return false;
		}
	}
	return true;
}

//----------------------------------------------------------------------------------------------

// validate inputs
if (isset($_POST['AddPaymentItem'])) {

	if (!can_process()) {
		unset($_POST['AddPaymentItem']);
	}
}

//ini_set('xdebug.dump.SERVER', 'HTTP_HOST, SERVER_NAME');

if (isset($_POST['AddPaymentItem'])) {
	if($isDP)
	add_dp(get_post('order_no'), input_num('amount'));
	else 
	update_lunas(get_post('order_no'), input_num('amount'));
	
	$temporder = get_post('order_no');
	exit();
	unset($_POST);
	unset($_SESSION);
	
	//$_GET['AddedID']=$orderno;
	$_POST['mode']='lunas';
	meta_forward($_SERVER['PHP_SELF'], "AddedID=".$temporder);
}


//----------------------------------------------------------------------------------------------
$new = 1;
$old_ref = 0;


	
start_form();

	start_outer_table(TABLESTYLE2, "width=60%", 5);
	table_section(1);

	//if ($new)
	if($isDP)
		sale_order_list_row(_("For Reference No:"), 'order_no', true, null, true);
	else 			
		sale_order_list_row(_("For Reference No:"), 'order_no', false, null, true);
	
		
	
		
	
if (isset($_POST['order_no']))
$arr = get_sales_order($_POST['order_no']);

$arrum = get_uang_muka($_POST['order_no']);
		table_section(2);
		
		label_cells(_("Total Order:"), price_format($arr['total']), "class='label'");
		if(!$isDP)
		{
			table_section(3);
			label_cells(_("Uang Muka:"), price_format($arrum->pay1), "class='label'");
		}
		end_outer_table(1);



		start_table(TABLESTYLE, "width=60%");

		amount_row(_("Amount:"), 'amount');

		textarea_row(_("Memo:"), 'memo_', null, 22, 4);
		end_table(1);

		br();

		if (isset($_POST['trans_no']) && $_POST['trans_no'] > 0 )
			submit_center('AddPaymentItem', _("Update Payment"), true, '', 'default');
		else
			submit_center('AddPaymentItem', _("Add Payment"), true, '', 'default');
   	
 	br();


end_form();
end_page();
?>
